#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

namespace my
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		T& operator[](size_t i)
		{
			assert(i < size());

			return _start[i];
		}

		const T& operator[](size_t i) const
		{
			assert(i < size());

			return _start[i];
		}

		void swap(vector<T>& tem)
		{
			std::swap(_start, tem._start);
			std::swap(_finish, tem._finish);
			std::swap(_endofstorage, tem._endofstorage);
		}

		vector<T>& operator=(vector<T> tem)
		{
			swap(tem);
			return *this;
		}

		vector()
		{}

		vector(initializer_list<T> li)
		{
			for (auto e : li)
			{
				push_back(e);
			}
		}

		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto e : v)
			{
				push_back(e);
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			while (n--)
			{
				push_back(val);
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			while (n--)
			{
				push_back(val);
			}
		}

		template<class Inputiterator>
		vector(Inputiterator begin, Inputiterator end)
		{
			while (begin != end)
			{
				push_back(*begin);
				begin++;
			}
		}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endofstorage = nullptr;
			}
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

		void resize(size_t n, const T& val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}

		void reserve(size_t n)
		{
			size_t oldsize = size();
			if (n > oldsize)
			{
				T* tem = new T[n];

				//memcpy(tem, _start, sizeof(T) * oldsize);

				if (_start)
				{
					for (int i = 0; i < oldsize; i++)
					{
						tem[i] = _start[i];
					}
					delete[] _start;
				}

				_start = tem;
				_finish = _start + oldsize;
				_endofstorage = _start + n;
			}
		}

		bool empty() const
		{
			return size() == 0;
		}

		void push_back(const T& x)
		{
			if (_finish == _endofstorage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}

			*_finish = x;
			_finish++;
		}

		void pop_back()
		{
			assert(!empty());

			_finish--;
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos < _finish);

			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}

			iterator i = _finish;
			while (i > pos)
			{
				*i = *(i - 1);
				i--;
			}
			*pos = x;
			_finish++;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			assert(!empty());

			iterator i = pos + 1;
			while (i != _finish)
			{
				*(i - 1) = *i;
				i++;
			}
			_finish--;
			return pos;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};
}